const cookie = new Cookie();
var token = cookie.getCookie('token');
//初始化页面函数
$(function () {
  //获取用户登录状态
  getUserStatu();
  //生成订单
  getOrderData()
})

//获取用户登录状态
function getUserStatu() {
  if (!cookie.getCookie('uid')) {
    $('.modal-body').html('<span>请回首页登陆</span>');
    $('#identifier').modal('show');
    setTimeout(function () {
      location.href = './idnex.html';
    }, 1500)
  } else {
    $('.username').text(cookie.getCookie('username'));
    $('.mycart').text(cookie.getCookie('username') + '的购物车');
  }
}

//请求订单数据
function getOrderData() {
  $.ajax({
    type: 'get',
    url: 'https://api.it120.cc/conner/order/list',
    data: {
      token
    }
  }).then(res => {
    const { goodsMap } = res.data
    let orderList = []
    for (let key in goodsMap) {
      orderList.push(goodsMap[key])
    }
    let str = ''
    orderList.forEach(item => {
      item.forEach(it=>{
        str += `
            <tr>
              <td>${it.goodsName}</td>
              <td><img src="${it.pic}" class="img"/></td>
              <td>${it.number}${it.unit}</td>
              <td>${it.dateAdd}</td>
              <td>${it.amount}</td>
              <td>${genderStatusTxt(it.status)}</td>
            </tr>
            `
      })
      $('tbody').append(str)
    })
    
  })
}

function genderStatusTxt(status) {
  switch (status) {
    case -1:
      return '<span class="order-close">已关闭</span>'
      break;
    case 0:
      return '<span class="wait-pay">待支付</span>'
      break;
    default:
      break;
  }
}
/* -1 已关闭 0 待支付 1 已支付待发货 2 已发货待确认 3 确认收货待评价 4 已评价 */